package com.dushibao.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dushibao.admin.model.Admin;
import com.dushibao.admin.model.Menu;
import com.dushibao.admin.mv.DushibaoModelAndView;
import com.dushibao.admin.service.IMenuService;
import com.dushibao.admin.vo.request.AdminModelPageRequest;
import com.dushibao.admin.vo.request.MenuModelPageRequest;
import com.dushibao.page.PageImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * Title: Menu.java
 *
 * Description:菜单 前端控制器
 *
 * Copyright: Copyright: Copyright (c) 2021
 *
 * Company: www.dushibao.com
 *
 * @author zqd
 *
 * @date 2021-12-07 08:28:35
 *
 * @version dushibao V1.0
 */
@Controller
public class MenuController {

    @Autowired
    IMenuService menuService;

    @RequestMapping("/menu/list")
    public ModelAndView list(MenuModelPageRequest modelPageRequest){
        ModelAndView mv = new DushibaoModelAndView("/menu/list",modelPageRequest);

        PageImpl<Menu> iPage = this.menuService.selectPageImpl(modelPageRequest);

        mv.addObject("iPage",iPage);

        return mv;
    }

    @RequestMapping(value = "/menu/del",method = RequestMethod.POST)
    @ResponseBody
    public Boolean del(@RequestBody List<Long> ids){

        this.menuService.removeByIds(ids);

        return true;
    }

    @RequestMapping("/menu/add")
    public ModelAndView add(Integer id){
        ModelAndView mv = new DushibaoModelAndView("/menu/add");

        if(id == null){
            return mv;
        }

        Menu obj = this.menuService.getById(id);

        mv.addObject("obj",obj);

        return mv;
    }

    @RequestMapping("/menu/save")
    public String save(Menu menu){
        this.menuService.saveOrUpdate(menu);

        return "redirect:/menu/list";
    }

    @RequestMapping(value = "/menu/validator",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> validator(Menu menu){
        Map<String,Object> ret = new HashMap<>();

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url",menu.getUrl());
        List<Menu> list = this.menuService.list(queryWrapper);

        if(list == null || list.size() == 0){
            ret.put("code",10000);
            ret.put("msg","验证通过");
        }else{
            ret.put("code",10001);
            ret.put("msg","菜单地址不能重复");
        }

        return ret;
    }

}
